/*
 * @(#)MaintDmsGroupRoleFolderAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsPersonalStorageSetup;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.core.DmsPersonalStorageSetupManager;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.dao.DmsPersonalStorageSetupDAObject;
import com.dcivision.dms.dao.DmsRootDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.user.bean.MtmUserGroupUserRole;
import com.dcivision.user.bean.MtmUserRecordUserGroup;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.MtmUserGroupUserRoleDAObject;
import com.dcivision.user.dao.MtmUserRecordUserGroupDAObject;
import com.dcivision.user.dao.MtmUserRecordUserRoleDAObject;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
  MaintDmsGroupRoleFolderAction.java

  This class is for maint dms paper document.

    @author          Jenny Li
    @company         DCIVision Limited
    @creation date   14/10/2004
    @version         $Revision: 1.12 $
*/

public class MaintDmsPersonalStorageSetupAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.12 $";
  protected String parentID = "";
  protected String rootID = "";

    
  public MaintDmsPersonalStorageSetupAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsPersonalStorageSetupDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_ADMIN);
  }
  
  /**
   *  Method execute() - generaic function of result list handling               
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public void selectRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    MaintDmsPersonalStorageSetupForm actionForm=(MaintDmsPersonalStorageSetupForm)form;
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn); 
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn);
    Integer ID=TextUtility.parseIntegerObj(actionForm.getID());
    DmsPersonalStorageSetup dmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getObjectByID(ID);
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    UserRecordDAObject userRecordDAO=new UserRecordDAObject(sessionContainer,conn);
    
    String memberName="";
    if(DmsPersonalStorageSetup.MEMBER_TYPE_USER.equals(dmsPersonalStorageSetup.getMemberType())){
      UserRecord userRecord=(UserRecord)userRecordDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
      memberName=userRecord.getFullName();
    }else if(DmsPersonalStorageSetup.MEMBER_TYPE_GROUP.equals(dmsPersonalStorageSetup.getMemberType())){
      UserGroup userGroup=(UserGroup)userGroupDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
      memberName=userGroup.getGroupName();
    }else if(DmsPersonalStorageSetup.MEMBER_TYPE_ROLE.equals(dmsPersonalStorageSetup.getMemberType())){
      UserRole userRole=(UserRole)userRoleDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
      memberName=userRole.getRoleName();
    }
    if("0.0".equals(dmsPersonalStorageSetup.getStorageLimit().toString())){
      actionForm.setUnlimit("Y");
      actionForm.setStorageLimit("");
    }
    dmsPersonalStorageSetup.setMemberName(memberName);
    actionForm.setFormData(dmsPersonalStorageSetup);
  }
  
  /**
   *  Method execute() - generaic function of result list handling               
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ServletException      Throws ServletException
   */
  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    MaintDmsPersonalStorageSetupForm actionForm=(MaintDmsPersonalStorageSetupForm)form;
    if("Y".equals(actionForm.getUnlimit())){
      actionForm.setStorageLimit("0.0");   
    }
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn); 
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn);
    DmsPersonalStorageSetup dmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.updateObject(actionForm.getFormData());
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    UserRecordDAObject userRecordDAO=new UserRecordDAObject(sessionContainer,conn);
    DmsRootDAObject dmsRootDAO=new DmsRootDAObject(sessionContainer,conn);
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);
    DmsPersonalStorageSetupManager dmsPersonalStorageSetupManager=new DmsPersonalStorageSetupManager(sessionContainer,conn);
    String memberName="";
    if(DmsPersonalStorageSetup.MEMBER_TYPE_USER.equals(dmsPersonalStorageSetup.getMemberType())){
      UserRecord userRecord=(UserRecord)userRecordDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
      memberName=userRecord.getFullName();
      DmsRoot dmsRoot=dmsRootDAO.getPersonalRootByOwnerIDAndType(userRecord.getID(),GlobalConstant.RECORD_STATUS_ACTIVE);
      if(!Utility.isEmpty(dmsRoot)){
        dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit()); 
      }
      dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
    }else if(DmsPersonalStorageSetup.MEMBER_TYPE_GROUP.equals(dmsPersonalStorageSetup.getMemberType())){
      UserGroup userGroup=(UserGroup)userGroupDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
      memberName=userGroup.getGroupName();
      List groupList=new ArrayList();
      groupList.add(0,userGroup.getID());
      List userList=mtmUserRecordUserGroupDAO.getListByUserGroupAry(groupList);
      for(int i=0;i<userList.size();i++){
        MtmUserRecordUserGroup mtmUserRecordUserGroup=(MtmUserRecordUserGroup)userList.get(i); 
        DmsPersonalStorageSetup userStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(mtmUserRecordUserGroup.getUserRecordID(),DmsPersonalStorageSetup.MEMBER_TYPE_USER);
        if(Utility.isEmpty(userStorageSetup) && !dmsPersonalStorageSetupManager.checkHasStorageRole(mtmUserRecordUserGroup.getUserRecordID())){
          DmsRoot dmsRoot=dmsRootDAO.getPersonalRootByOwnerIDAndType(mtmUserRecordUserGroup.getUserRecordID(),GlobalConstant.RECORD_STATUS_ACTIVE);
          if(!Utility.isEmpty(dmsRoot)){
            List sotrageGroupList=dmsPersonalStorageSetupDAO.getStorageGroupListByUserID(mtmUserRecordUserGroup.getUserRecordID());
            if(sotrageGroupList.size()>0){
              Double oldGroupStorageLimit=((DmsPersonalStorageSetup)sotrageGroupList.get(0)).getStorageLimit();
              if(!"0.0".equals(dmsPersonalStorageSetup.getStorageLimit().toString())){
                if(dmsPersonalStorageSetup.getStorageLimit().equals(oldGroupStorageLimit)){
                  dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
                  dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
                }
              }else{
                dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
                dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot); 
              }
            }else{
              dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
              dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);   
            }
          }
        }
      }
    }else if(DmsPersonalStorageSetup.MEMBER_TYPE_ROLE.equals(dmsPersonalStorageSetup.getMemberType())){
      UserRole userRole=(UserRole)userRoleDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
      memberName=userRole.getRoleName();
      List userList=mtmUserRecordUserRoleDAO.getUserIDListByUserRoleID(userRole.getID());
      List roleList=new ArrayList();
      roleList.add(0,dmsPersonalStorageSetup.getMemberID());
      List groupList=mtmUserGroupUserRoleDAO.getListByUserRoleAry(roleList);
      for(int j=0;j<groupList.size();j++){
        MtmUserGroupUserRole mtmUserGroupUserRole=(MtmUserGroupUserRole)groupList.get(j);
        List groupUserList=mtmUserRecordUserGroupDAO.getUserIDListByUserGroupID(mtmUserGroupUserRole.getUserGroupID());
        for(int k=0;k<groupUserList.size();k++){
          Integer userRecordID=(Integer)groupUserList.get(k);
          userList.add(userRecordID);
        }
      }
      
      java.util.Hashtable hst=new java.util.Hashtable();
      List newUserIDList=new ArrayList();
      for(int j=0;j<userList.size();j++){
        Integer userID=(Integer)userList.get(j);
        hst.put(userID,userID);
      }
      if(hst.size()>0){
        java.util.Enumeration e = hst.elements();
        int j = 0;
        newUserIDList.add(0,e.nextElement());
        while (e.hasMoreElements()) {
          j++;
          newUserIDList.add(j,e.nextElement());
        }
      }

      for(int i=0;i<newUserIDList.size();i++){
        Integer userRecordID=(Integer)newUserIDList.get(i); 
        DmsPersonalStorageSetup userStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(userRecordID,DmsPersonalStorageSetup.MEMBER_TYPE_USER);
        if(Utility.isEmpty(userStorageSetup)){
          DmsRoot dmsRoot=dmsRootDAO.getPersonalRootByOwnerIDAndType(userRecordID,GlobalConstant.RECORD_STATUS_ACTIVE);
          if(!Utility.isEmpty(dmsRoot)){
            List sotrageRoleList=dmsPersonalStorageSetupDAO.getStorageRoleListByUserID(userRecordID);
            if(sotrageRoleList.size()>0){
              Double oldRoleStorageLimit=((DmsPersonalStorageSetup)sotrageRoleList.get(0)).getStorageLimit();
              if(!"0.0".equals(dmsPersonalStorageSetup.getStorageLimit().toString())){
                if(dmsPersonalStorageSetup.getStorageLimit().equals(oldRoleStorageLimit)){
                  dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
                  dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
                }
              }else{
                dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
                dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot); 
              }
            }else{
              dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
              dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);   
            }
          }
        }
      }
    }
    dmsPersonalStorageSetup.setMemberName(memberName);
    actionForm.setFormData(dmsPersonalStorageSetup);
  }
  
}